﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StudyLibrary.MenuForm;
using Model;
using DBServer;

namespace StudyLibrary
{
    public partial class frmMenuManage : Form
    {
        public bool _isSuccess = false;
        public frmMenuManage()
        {
            InitializeComponent();
        }

        private void frmMenuManage_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            BindDGV();
        }

        private void BindDGV()
        {
            string sql = "SELECT * FROM Menu";
            DataSet ds = SQLiteHelper.ExecuteDataset(sql);
           
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Refresh();
        }

        private void tsBtn_Add_Click(object sender, EventArgs e)
        {
            frmMenuDetail f = new frmMenuDetail(0, null);
            f.ShowDialog();
            if (f._isSuccess)
            {
                BindDGV();
            }
        }

        private void tsBtn_Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0)
            {
                MENU menu = new MENU();

                menu.Name = dataGridView1.SelectedRows[0].Cells["cName"].Value.ToString();

                //string sql = "DELETE FROM Menu WHERE Name = '" + menu.Name + "'";
                //int re = SQLiteHelper.ExecuteNonQuery(sql);
                int re = NHibernateHelper.DeleteObject(menu);
                if (re > 0)
                {
                    MessageBox.Show("Successful");
                    BindDGV();
                    _isSuccess = true;
                }
                else
                {
                    MessageBox.Show("Failure");
                }
            }
        }

        private void tsBtn_Update_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0)
            {
                MENU menu = new MENU();

                menu.Name = dataGridView1.SelectedRows[0].Cells["cName"].Value.ToString();
                menu.Path = dataGridView1.SelectedRows[0].Cells["cPath"].Value.ToString();
                menu.ViewOrder = (int)dataGridView1.SelectedRows[0].Cells["cOrder"].Value;

                frmMenuDetail f = new frmMenuDetail(1, menu);
                f.ShowDialog();
                if (f._isSuccess)
                {
                    BindDGV();
                }
            }
        }
    }
}
